package lt.web.controller.end;


import lt.web.pojo.AppCategory;
import lt.web.pojo.AppInfo;
import lt.web.pojo.AppVersion;
import lt.web.pojo.DataDictionary;
import lt.web.service.AppCategoryService;
import lt.web.service.AppInfoService;
import lt.web.service.AppVersionService;
import lt.web.service.DataDictionaryService;
import lt.web.utils.Constants;
import lt.web.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/manager/backend")
public class BackendUserController {

    @Autowired
    AppInfoService appInfoService;

    @Autowired
    AppCategoryService appCategoryService;

    @Autowired
    DataDictionaryService dataDictionaryService;

    @Autowired
    AppVersionService appVersionService;


    //<editor-fold desc="首页">
    @RequestMapping("/list")
    public String getAppInfoList(Model model,
                                 @RequestParam(value = "querySoftwareName", required = false) String softwarename,
                                 @RequestParam(value = "queryFlatformId", required = false) Integer flatFormId,
                                 @RequestParam(value = "queryCategoryLevel1", required = false) Integer categoryLevel1,
                                 @RequestParam(value = "queryCategoryLevel2", required = false) Integer categoryLevel2,
                                 @RequestParam(value = "queryCategoryLevel3", required = false) Integer categoryLevel3,
                                 @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex
    ) {

        Integer pageSize = Constants.pageSize;//页面容量
        //获取记录总数
        int totalCount = appInfoService.getAppInfoCount(softwarename, 1, categoryLevel1, categoryLevel2, categoryLevel3, flatFormId, null);

        PageSupport pageSupport = new PageSupport();

        pageSupport.setCurrentPageNo(pageIndex);//当前页码
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);//总记录数
        int totalPageCount = pageSupport.getTotalPageCount();//总页数

        //控制首页和尾页
        if (pageIndex < 1) {
            pageIndex = 1;
        } else if (pageIndex > totalPageCount) {
            pageIndex = totalPageCount;
        }

        //当前页面记录列表集合
        List<AppInfo> appInfoList = appInfoService.selectAllByCondation(softwarename, 1, categoryLevel1, categoryLevel2, categoryLevel3, null,flatFormId, pageIndex, pageSize);

        //一级分类列表
        List<AppCategory> categoryList1 = appCategoryService.getByCategorynameAppCategoryList(null);

        //获取平台列表
        List<DataDictionary> flatFormList = dataDictionaryService.getDataDictionaryList("APP_FLATFORM");

        model.addAttribute("pages", pageSupport);
        model.addAttribute("appInfoList", appInfoList);
        model.addAttribute("querySoftwareName", softwarename);
        model.addAttribute("queryCategoryLevel1", categoryLevel1);
        model.addAttribute("queryCategoryLevel2", categoryLevel2);
        model.addAttribute("queryCategoryLevel3", categoryLevel3);
        model.addAttribute("queryFlatformId", flatFormId);
        model.addAttribute("categoryLevel1List", categoryList1);
        model.addAttribute("flatFormList", flatFormList);

        //二级分类列表和三级分类列表---回显
        if(categoryLevel2 != null ){
            List<AppCategory> queryCategoryLevel2 = appCategoryService.getByCategorynameAppCategoryList(categoryLevel1);
            model.addAttribute("categoryLevel2List", queryCategoryLevel2);
        }
        if(categoryLevel3 != null){
            List<AppCategory> queryCategoryLevel3 = appCategoryService.getByCategorynameAppCategoryList(categoryLevel2);
            model.addAttribute("categoryLevel3List", queryCategoryLevel3);
        }
        return "/backend/applist";
    }

    //级联二级分类
    @RequestMapping("/categorylevellist.json")
    @ResponseBody
    public Object getCategoryLevelListByParentId(@RequestParam("pid") Integer parentId) {
        List<AppCategory> categoryList = appCategoryService.getByCategorynameAppCategoryList(parentId);
        return categoryList;
    }
    //</editor-fold>


    //<editor-fold desc="审核功能">
    //获取app信息，显示到审核页面
    @RequestMapping("/getCheckInfo")
    public String getCheckInfo(Model model,
                               @RequestParam("vid") Integer vid,
                               @RequestParam("aid") Integer aid){
        AppVersion appVersion = appVersionService.getById(vid);
        AppInfo appInfo = appInfoService.getAppInfoById(aid);

        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersion",appVersion);

        return "/backend/appcheck";
    }

    //审核app状态
    @RequestMapping("/check")
    public String checkAppStatus(@RequestParam("id") Integer id,
                                 @RequestParam("status") Integer status){
        int result =  appInfoService.updateAppInfoStatus(status, id);
        if (result>0){
            return "redirect:/manager/backend/list";
        }
        else{
            return "/backend/appcheck";
        }
    }
    //</editor-fold>
}
